package com.bluse.service;

import com.bluse.entity.UserEntity;
import com.bluse.repository.UserRepository;
import com.bluse.service.dto.UserDto;
import com.bluse.service.mapper.UserMapper;
import org.springframework.boot.autoconfigure.flyway.FlywayDataSource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Describe: TODO
 * @Author: bluse
 * @Date: 2018/12/24 11:20
 **/

//@Service
public class UserService {

    @Resource
    private final UserMapper userMapper;

    private final UserRepository userRepository;


    public UserService(UserMapper userMapper, UserRepository userRepository) {
        this.userMapper = userMapper;
        this.userRepository = userRepository;
    }

    /**
     * find all user
     *
     * @return
     */
    public List<UserDto> findAll() {
        List<UserEntity> userEntities = userRepository.findAll();
        return userEntities.stream().map(userMapper::toDto).collect(Collectors.toList());
    }

    /**
     * find a user by uid
     *
     * @param id
     * @return
     */
    public UserDto findUserById(String id) {
        return userRepository.findById(id).map(userMapper::toDto).get();
    }

    /**
     * save a user
     *
     * @param userDto
     */
    public UserDto save(UserDto userDto) {
        UserEntity entity = userMapper.toEntity(userDto);
        return userMapper.toDto(userRepository.save(entity));
    }


    /**
     * delete one user by  user id
     *
     * @param userDto
     */
    public void deleteUser(UserDto userDto) {
        userRepository.delete(userMapper.toEntity(userDto));
    }


}
